package com.snail.readyvr.fragment;

import android.content.ClipboardManager;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.interfaces.MaxLengthWatcherListener;
import com.snail.readyvr.mvp.contract.AddVideoActivityContract;
import com.snail.readyvr.mvp.model.VideoHistory;
import com.snail.readyvr.util.MaxLengthWatcher;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.StringUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snail.readyvr.util.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by MRKING on 2016/11/17.
 */

public class AddVideoFragment extends BaseFragment implements MaxLengthWatcherListener, AddVideoActivityContract.View {
    @BindView(R.id.add_new_video_edit)
    EditText mAddNewVideoEdit;
    @BindView(R.id.add_new_video_paste)
    Button mAddNewVideoPaste;

    @BindView(R.id.add_new_video_player)
    Button mAddNewVideoPlayer;
    @BindView(R.id.clear)
    TextView mClearText;
    @BindView(R.id.url_tip)
    TextView mUrlTip;

    private boolean editHasText = false;

    private boolean mHasFoucs = false;

    private AddVideoActivityContract.Presenter mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_video;
    }

    @Override
    protected void init() {

        mUrlTip.setText(Html.fromHtml(getString(R.string.add_url_type_tip_start) + "<font color='#8a8a8a'>" + getString(R.string.add_url_type_tip_end) + "</font>"));
        MaxLengthWatcher maxLengthWatcher = new MaxLengthWatcher(200, mAddNewVideoEdit);
        maxLengthWatcher.setMaxLengthWatcherListener(this);
        mAddNewVideoEdit.addTextChangedListener(maxLengthWatcher);
        mAddNewVideoEdit.setActivated(false);

        mAddNewVideoEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mHasFoucs = hasFocus;
                showClear();
            }
        });
    }

    private void showClear() {
        if (mClearText != null){
            if (mHasFoucs && editHasText){
                mClearText.setVisibility(View.VISIBLE);
            }else{
                mClearText.setVisibility(View.GONE);
            }

        }
    }

    @OnClick({R.id.add_new_video_paste, R.id.add_new_video_scan, R.id.add_new_video_player})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_new_video_paste:
                paste();
                break;
            case R.id.add_new_video_scan:
                StartActivityUtils.startScannerActivity(getContext());
                getActivity().finish();
                break;
            case R.id.add_new_video_player:
                addVideo();
                break;
        }
    }

    @OnClick(R.id.clear)
    public void onClickClear(){
        mAddNewVideoEdit.setText(null);
        mClearText.setVisibility(View.GONE);
    }

    private void addVideo() {
        if (!TextUtils.isEmpty(mAddNewVideoEdit.getText())) {
            String url = mAddNewVideoEdit.getText().toString().trim();
            if (Utils.isVideoUrl(url)) {
                String userName = "visitor";
                if (Utils.sUser != null && !StringUtils.isBlank(Utils.sUser.getEmailOrPhone()))
                    userName = Utils.sUser.getEmailOrPhone();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                VideoHistory videoHistory = new VideoHistory(null,userName,url,format.format(new Date()));
                mPresenter.addVideo(videoHistory);
                ToastUtils.show(getContext(), R.string.add_video_url_success);
                StartActivityUtils.startVideoPlayer(getContext(),url);
                getActivity().finish();
            } else {
                ToastUtils.show(getContext(), R.string.url_not_ok_tip);
            }
        }else {
            ToastUtils.show(getContext(), R.string.url_not_ok_tip);
        }

    }

    private void paste() {
        ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);


        if (!TextUtils.isEmpty(cmb.getText())) {
            String text = cmb.getText().toString().trim();
            if (Utils.isVideoUrl(text)) {
                mAddNewVideoEdit.setText(text);
            } else {
                ToastUtils.show(getContext(), R.string.add_new_video_paste_tip);
            }
        } else {
            ToastUtils.show(getContext(), R.string.add_new_video_paste_tip);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void overMaxLength(View view, int max) {

    }

    @Override
    public void lengthChanged(View view, int length) {
        editHasText = length > 0 ? true : false;
        showClear();
        if (length > 0) {
            mAddNewVideoPlayer.setEnabled(true);
        }else {
            mAddNewVideoPlayer.setEnabled(false);
        }
    }


    @Override
    public void setPresenter(AddVideoActivityContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void findAllVideoHistorySuccess(List<VideoHistory> videoHistories) {

    }

    @Override
    public void deleteAllVideosSuccess() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {
        ToastUtils.show(getContext(), throwable.getMessage() + "");
    }
}
